package org.niugang.config;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

/**
 * 请求拦截
 * Copyright: Copyright (c) 2018 863263957@qq.com
 * 
 * @ClassName: AccessFilter.java
 * @version: v1.0.0
 * @author: niugang
 * @date: 2018年9月19日 下午4:04:07
 */

public class AccessFilter extends ZuulFilter {
	private final Logger logger = LoggerFactory.getLogger(AccessFilter.class);

	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public String filterType() {
		return "pre";
	}

	@Override
	public int filterOrder() {
		return 0;
	}

	@Override
	public Object run() {
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();
		try {
	
			logger.info("send  {} request to {} ", request.getMethod(), request.getRequestURL().toString());
			//先校验是否有access_token
			String accessToken = request.getParameter("access_token");
		
			if (accessToken == null) {
				logger.warn("access token is empty");
				ctx.setSendZuulResponse(false);
				ctx.setResponseStatusCode(401);
				ctx.setResponseBody("no power");
				return null;
			}
	
			logger.info("access token ok");
	
		} catch (Exception e) {
		
		}

		return null;
	}

}
